<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="用户号" prop="tradeNo">
                <el-input
                        v-model="queryParams.tradeNo"
                        placeholder="请输入用户号"
                        clearable
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="买家昵称" prop="userName">
                <el-input
                        v-model="queryParams.userName"
                        placeholder="请输入买家昵称"
                        clearable
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                >新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="info"
                    icon="el-icon-s-home"
                    size="mini"
                    @click="handleBack"
                >返回</el-button>
            </el-col>
        </el-row>

        <el-table v-loading="loading" :data="omsList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column
                label="序号"
                type="index"
                width="50">
            </el-table-column>
            <el-table-column label="用户ID" align="center" prop="id" />
            <el-table-column label="系统账号" align="center" prop="jkNumber" />
            <el-table-column label="账户" align="center" prop="userName" />
            <el-table-column label="密码" align="center" prop="passWord" />
            <el-table-column label="AppKey" align="center" prop="appKey" />
            <el-table-column label="AppSecret" align="center" prop="appSecret" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                    >修改</el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 添加或修改用户号对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form"  label-width="100px">
                <el-form-item label="系统账号：">
                    <el-input v-model="form.jkNumber" clearable></el-input>
                </el-form-item>
                <el-form-item label="账户：">
                    <el-input v-model="form.userName" clearable></el-input>
                </el-form-item>
                <el-form-item label="密码：">
                    <el-input v-model="form.passWord" clearable></el-input>
                </el-form-item>
                <el-form-item label="AppKey：">
                    <el-input v-model="form.appKey" clearable></el-input>
                </el-form-item>
                <el-form-item label="AppSecret：">
                    <el-input v-model="form.appSecret" clearable></el-input>
                </el-form-item>
                <el-form-item label="flagId：">
                    <el-input v-model="form.flagId" clearable></el-input>
                </el-form-item>
                <el-form-item label="flagName：">
                    <el-input v-model="form.flagName" clearable></el-input>
                </el-form-item>
                <el-form-item label="角色：">
                    <el-select v-model="form.role" placeholder="请选择">
                        <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { deleteUserApi,
    updateUserApi,
    listUserApi,
    saveUserApi, } from "@/api/user.js";

export default {
    name: "business",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 用户号表格数据
            omsList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                tradeNo:null,
                userName:null,
            },
            // 表单参数
            form: {},
            options: [{
                value: '1',
                label: '其他'
            },{
                value: '0',
                label: '管理员'
            }, ]
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询用户号列表 */
        getList() {
            this.loading = true;
            listUserApi(this.queryParams).then(response => {
                this.omsList = response.result;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                role: null,
                appKey: null,
                appSecret: null,
                jkNumber: null,
                userName: null,
                passWord: null,
                flagName: null,
                flagId: null,
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.queryParams = {
                pageNum: 1,
                    pageSize: 10,
                    tradeNo:null,
                    userName:null,
            }
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length!==1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加用户号";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            // const id = row.id || this.ids
            // updateUserApi(id).then(response => {
                this.form = {...row};
                this.open = true;
                this.title = "修改用户信息";
            // });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateUserApi(this.form).then(response => {
                            this.$message.success('修改成功')
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        saveUserApi(this.form).then(response => {
                            this.$message.success('新增成功')
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function() {
                return deleteUserApi(ids);
            }).then(() => {
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
                this.getList()
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        handleBack(){
            this.$router.back()
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/oms/export', {
                ...this.queryParams
            }, `oms_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
<style>

.pass{
    display: inline-block;
    position: relative;
    width: 65px;
    height: 28px;
    border-radius: 5px;
    border: 3px solid;
    font-size: 16px;
    text-align: center;
    line-height: 22px;
    color: #22b246;
    text-transform: uppercase;
//transform: rotate(5deg);
    z-index: 0;
}

.pass:after{
    content: "上传成功";
    text-transform: uppercase;
    font-weight: bold;
    z-index: 0;
}
.not-pass{
    display: inline-block;
    position: relative;
    width: 65px;
    height: 28px;
    border-radius: 5px;
    border: 3px solid;
    font-size: 13px;
    text-align: center;
    line-height: 22px;
    color: #b22222;
    text-transform: uppercase;
//transform: rotate(5deg);
    z-index: 0;
}
.not-pass:after{
    content: "上传失败";
    text-transform: uppercase;
    font-weight: bold;
    z-index: 0;
}
</style>
